import Vue from 'vue'
import Router from 'vue-router'
import Nprogress from 'nprogress'
import Login from '@/view/Login'
import Home from '@/view/Home'
import Welcome from '@/view/Welcome'
import User from '@/view/user/User'
import Roles from '@/view/power/Roles'
import Rights from '@/view/power/Rights'
import Cate from '@/view/goods/Cate'
import Params from '@/view/goods/Params'
import GoodsList from '../view/goods/List'
import GoodsAdd from '../view/goods/Add'
import GoodsEdit from '../view/goods/Edit'
import Order from '../view/order/Order'
import Reports from '../view/report/Report'

Vue.use(Router)
const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      redirect: '/welcome',
      component: Home,
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/home/users', component: User },
        { path: '/home/roles', component: Roles },
        { path: '/home/rights', component: Rights },
        { path: '/home/categories', component: Cate },
        { path: '/home/params', component: Params },
        { path: '/home/goods', component: GoodsList },
        { path: '/home/goods/add', component: GoodsAdd },
        { path: '/home/goods/edit/:id', component: GoodsEdit },
        { path: '/home/orders', component: Order },
        { path: '/home/reports', component: Reports }
      ]
    }
  ]
})
// 挂载路由导航
router.beforeEach((to, from, next) => {
  Nprogress.start()
  if (to.path === '/welcome') {
    window.localStorage.removeItem('activePath')
  }
  // 当要跳到登录组件的时候  直接放行
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  // 判断token值是否存在   如果跳转组件的时候 token值不存在直接跳到登录页
  if (!token) return next('/login')
  // 直接放行
  next()
})

router.afterEach((to, from) => {
  Nprogress.done()
})
export default router
